package com.ayz.gateway.center.infrastructure.nginx.Executor;

import com.ayz.gateway.common.constants.GatewayServiceData;
import com.github.odiszapc.nginxparser.NgxBlock;
import com.github.odiszapc.nginxparser.NgxConfig;
import com.github.odiszapc.nginxparser.NgxEntry;
import com.github.odiszapc.nginxparser.NgxParam;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
 * @ClassName GatewayServiceOffline
 * @Description TODO: 网关服务下线器
 * @Author 快乐的星球
 * @Date 2023/6/12 17:29
 * @Version 1.0
 **/
@Getter
@Setter
public class GatewayServiceOffline {


    public void offlineGatewayService(GatewayServiceData gatewayServiceData,
                                      NgxConfig ngxConfig){
        //1:是否当前业务组下最后一个下线的网关服务
        boolean isLasted=false;
        NgxBlock upstreamBlock=null;
        String groupName=gatewayServiceData.getGatewayGroup();
        String serverAddress=gatewayServiceData.getAddress();

        List<NgxEntry> upstreamList = ngxConfig.findAll(NgxConfig.BLOCK, "upstream");
        if(upstreamList!=null && !upstreamList.isEmpty()){
            for (NgxEntry upstreamEntry : upstreamList) {
                NgxBlock curUpstream=(NgxBlock) upstreamEntry;
                if(groupName.equals(curUpstream.getValue())){
                    upstreamBlock=curUpstream;
                    break;
                }
            }
        }
        if(upstreamBlock!=null){
            //2: 完成当前网关业务组下当前网关服务的下线
            NgxParam remove=null;
            List<NgxEntry> serverList = upstreamBlock.findAll(NgxConfig.PARAM, "server");
            for (NgxEntry serverEntry : serverList) {
                NgxParam curServer=(NgxParam) serverEntry;
                if(serverAddress.equals(curServer.getValue())){
                    remove=curServer;
                    break;
                }
            }
            if(remove!=null){
                upstreamBlock.remove(remove);
            }
            //3:判断当前节点是否是最后一个
            serverList=upstreamBlock.findAll(NgxConfig.PARAM, "server");
            if(serverList==null || serverList.isEmpty()){
                isLasted=true;
                ngxConfig.remove(upstreamBlock);
            }
            if(isLasted){
                //4:移除路由项数据
                String routeKey="/"+groupName+"/";
                NgxBlock removeBlock=null;

                List<NgxEntry> routeList = ngxConfig.findAll(NgxConfig.BLOCK, "server", "location");
                for (NgxEntry routeEntry : routeList) {
                    NgxBlock curRoute=(NgxBlock) routeEntry;
                    String curRouteKey = curRoute.getValue();
                    if(routeKey.equals(curRouteKey)){
                        removeBlock=curRoute;
                        break;
                    }
                }
                if(removeBlock!=null){
                    NgxBlock serverBlock = ngxConfig.findBlock("server");
                    serverBlock.remove(removeBlock);
                }
            }
        }
    }
}
